<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>书籍管理页</title>
<%@include file="includeHead.jsp"%>

</head>
<body>
	<table id="main_form" class="easyui-datagrid" title="书籍列表"
		data-options="pagination:true,rownumbers:true,singleSelect:true,url:'${pageContext.request.contextPath}/GetBookList',method:'get',toolbar:toolbarTop">
		<thead>
			<tr>
				<th data-options="field:'bookID',width:80">书籍编号</th>
				<th data-options="field:'bookName',width:100">书名</th>
				<th data-options="field:'bookAuthor',width:100">书籍作者</th>
				<th data-options="field:'bookPub',width:80,align:'right'">书籍出版社</th>
				<th data-options="field:'bookPubDate',width:80,align:'right'">书籍出版时间</th>
				<th data-options="field:'bookPage',width:80,align:'right'">书籍页数</th>
				<th data-options="field:'bookISBN',width:80,align:'right'">书籍ISBN</th>
				<th data-options="field:'bookAddDate',width:80,align:'right'">书籍添加日期</th>
			</tr>
		</thead>
	</table>

	<div id="addDiv" class="easyui-window" title="添加用户"
		data-options="iconCls:'icon-save',closed:true,modal:true,shadow:true,resizable:false"
		style="width: 500px; height: 450px; padding: 5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding: 10px;">
				<form id="addForm" method="post" enctype="multipart/form-data">
					<table cellpadding="5">
						<tr style="display:none">
							<td>书籍编号:</td>
							<td><input id="form_bookID" name="bookID"
								data-options="required:true"
								readonly=true></input></td>
						</tr>
						<tr>
							<td>书名:</td>
							<td><input id="form_bookName" name="bookName"
								data-options="required:true,validType:'minLength[5]'"
								></input></td>
						</tr>
						<tr>
							<td>书籍作者:</td>
							<td><input id="form_bookAuthor" name="bookAuthor"
								data-options="required:true"></input></td>
						</tr>
						<tr>
							<td>书籍出版社:</td>
							<td><input name="bookPub" id="form_bookPub"
								data-options="multiline:false"></input></td>
						</tr>
						<tr>
							<td>书籍出版时间:</td>
							<td><input name="bookPubDate" id="form_bookPubDate"
								class="easyui-datebox"
								data-options="multiline:false,validType:'timeV'"></input></td>
						</tr>
						<tr>
							<td>书籍页数:</td>
							<td><input name="bookPage" id="form_bookPage"
								data-options="multiline:true"></input></td>
						</tr>
						<tr>
							<td>书籍ISBN:</td>
							<td><input name="bookISBN" id="form_bookISBN"
								data-options="multiline:false"></input></td>
						</tr>
						<tr>
							<td>书籍添加日期:</td>
							<td><input name="bookAddDate" id="form_bookAddDate"
								class="easyui-datebox" data-options="multiline:true"></input></td>
						</tr>
						<tr>
							<td>书籍内容简介:</td>
							<td><input name="bookContent" id="form_bookContent"
								data-options="multiline:true"></input></td>
						</tr>
						<tr>
							<td>书籍目录:</td>
							<td><input name="bookCatalogue" id="form_bookCatalogue"
								data-options="multiline:true"></input></td>
						</tr>
						<tr>
							<td>书籍图片:</td>
							<td><input name="bookImg" id="form_bookImg" type="file" /></td>
						</tr>
					</table>
				</form>
			</div>
			<div data-options="region:'south',border:false"
				style="text-align: right; padding: 5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'"
					href="javascript:void(0)" onclick="" id="btn_Submit"
					style="width: 80px">确认</a> <a class="easyui-linkbutton"
					data-options="iconCls:'icon-cancel'" href="javascript:void(0)"
					onclick="$('#addDiv').window('close');" style="width: 80px">取消</a>
			</div>
		</div>
	</div>

	<script type="text/javascript">
		$.extend($.fn.validatebox.defaults.rules, {
			timeV : {
				validator : function(value) {
					var result = value
							.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
					if (result == null)
						return false;
					return true
				},
				message : '日期有误！请重新输入！.'
			},
			minLength : {
				validator : function(value, param) {
					return value.length >= param[0];
				},
				message : 'Please enter at least {0} characters.'
			}
		});
		/*日期控件本地化*/
		var options = {
			required : "true",
			missingMessage : "必填项",
			formatter : function(date) {
				var y = date.getFullYear();
				var m = date.getMonth() + 1;
				var d = date.getDate();
				return y + "-" + (m < 10 ? ("0" + m) : m) + "-"
						+ (d < 10 ? ("0" + d) : d);
			}
		}
		$("#form_bookPubDate").datebox(options);
		$("#form_bookAddDate").datebox(options);

		var toolbarTop = [
				{
					text : '添加',
					iconCls : 'icon-add',
					handler : function() {
						$('#addDiv').form('clear');
						$("#addDiv").panel({
							title : "添加书籍"
						});
						var now = new Date();
						$('#form_bookAddDate')
								.datebox(
										'setValue',
										now.getYear()
												+ "-"
												+ ((now.getMonth() + 1) < 10 ? "0"
														: "")
												+ (now.getMonth() + 1)
												+ "-"
												+ (now.getDate() < 10 ? "0"
														: "") + now.getDate());
						$('#btn_Submit').attr("onclick", " cmd('add');")
						$('#addDiv').window('open');
					}
				},
				{
					text : '修改',
					iconCls : 'icon-cut',
					handler : function() {
						$('#addDiv').form('clear');
						var row = $('#main_form').datagrid('getSelected');

						if (row != null) {
							$("#form_bookName").val(row.bookName);
							$("#form_bookAuthor").val(row.bookAuthor);
							$("#form_bookPub").val(row.bookPub);
							$("#form_bookPubDate").datebox("setValue",
									row.bookPubDate);
							$("#form_bookPage").val(row.bookPage);
							$("#form_bookISBN").val(row.bookISBN);
							$("#form_bookAddDate").datebox("setValue",
									row.bookAddDate);
							$("#form_bookContent").val(row.bookContent);
							$("#form_bookCatalogue").val(row.bookCatalogue);
							$('#form_bookID').val(row.bookID);
							$("#addDiv").panel({
								title : "修改书籍"
							});
							$('#btn_Submit').attr("onclick", " cmd('modify');")
							$('#addDiv').window('open');

						} else
							alert('请选择要修改的书籍');

					}
				},
				'-',
				{
					text : '删除',
					iconCls : 'icon-save',
					handler : function() {
						var row = $('#main_form').datagrid('getSelected');

						if (row != null) {
							$
									.post(
											"${pageContext.request.contextPath }/AdminBookControlServlet?control=delete&bookId="
													+ row.bookID, null,
											function(result) {
												if (result != 'success') {
													alert(result);
												} else {
													alert("删除成功");
													closeForm();
													$('#main_form').datagrid(
															'reload');
												}
											}, "text");
						} else
							alert('请选择要删除的书籍');
					}
				} ];
		function cmd(c) {
			if ($("#addForm").form('enableValidation').form('validate')) {
				if (c == "add") {
					addFun();
				}
				if (c == "modify") {
					modifyFun();
				}
			}
		}
		function modifyFun() {

			$("#addForm")
			.attr("action",
					"${pageContext.request.contextPath }/AdminBookControlServlet?control=modify");
			$('#addForm').submit();
			
			/*
			var d = $("#addForm").serialize();
			d = d + "&bookId=" + $('#main_form').datagrid('getSelected').bookID;

			$
					.post(
							"${pageContext.request.contextPath }/AdminBookControlServlet?control=modify",
							d, function(result) {
								if (result != 'success') {
									alert(result);
								} else {
									alert("修改成功");
									closeForm();
									$('#main_form').datagrid('reload');
									$('#addUserDiv').form('clear');
								}
							}, "text");
			*/

		}
		function addFun() {
			$("#addForm")
					.attr("action",
							"${pageContext.request.contextPath }/AdminBookControlServlet?control=add");
			$('#addForm').submit();

			/*
			
			
			var d = $("#addForm").serialize();
			$
					.post(
							"${pageContext.request.contextPath }/AdminBookControlServlet?control=add",
							d, function(result) {
								if (result != 'success') {
									alert(result);
								} else {
									alert("添加成功");

									closeForm();
									$('#main_form').datagrid('reload');
								}
							}, "text");
			 */
		}

		function closeForm() {
			$('#addDiv').window('close');
		}
	</script>

</body>
</html>